package telnetserver;

import java.net.*;
import java.util.ArrayList;

public class Telnet {
    private ArrayList<User> users;
    private Listener listener;
    private Login login;
    private Long startTime;
    private int most;
    private int[][] weapon;  // 0 = damage  1 = radius  2 = price
    
    public static void main(String[] args) {
        new Telnet();
    }
    
    public Telnet() {
        weapon = new int[3][3];
        // 0 = EXITBUTTON!
        // MISSILE!
        weapon[1][0] = 100;
        weapon[1][1] = 2;
        weapon[1][2] = 400;
        //NUKE!
        weapon[2][0] = 200;
        weapon[2][1] = 4;
        weapon[2][2] = 800;
        
        
        startTime = System.currentTimeMillis();
        most = 0;
        Menu menu = new Menu(this);
        Chat chat = new Chat(this, menu);
        About about = new About(this, menu);
        Info info = new Info(this, menu);
        Sources sources = new Sources(this, menu);
        Snake snake = new Snake(this, menu);
        Exit exit = new Exit();
        Object[] o = { chat, about, info, sources, snake, exit };
        menu.setMenu(o);
        login = new Login(this, menu);
        listener = new Listener();
        listener.start();
        users = new ArrayList<User>();
        try {
            if (System.in.read() >= 0) stopServer();
        } catch (Exception e) {}
    }
    
    public void stopServer() {
        listener.stopListener();
        for (User u : users) {
            if (u != null) u.closeSocket();
        }
        System.exit(0);
    }
    
    public void addUser(Socket s) {
        User u = new User(this, s, login);
        users.add(u);
        u.start();
        login.addUser(u);
        int i = getUsersOnline();
        if (i > getMostUsers()) setMostUsers(i);
    }
    
    public void remUser(User u) {
        try {
            Class[] parameterTypes = { User.class };
            u.getMode().getClass().getMethod("remUser", parameterTypes).invoke(u.getMode(), u);
        } catch (Exception e) {}
        users.remove(u);
    }
    
    public boolean userExists(String nick) {
        for (User u : users) {
            if (u.getNick().equalsIgnoreCase(nick)) return true;
        }
        return false;
    }
    
    public void changeMode(User u, Object o) {
        try {
            u.setMode(o);
            Class[] parameterTypes = { User.class };
            o.getClass().getMethod("addUser", parameterTypes).invoke(o, u);
        } catch (Exception e) {}
    }
    
    public String skin(int x, int y, String s) {
        return (char)27 + "[" + y + ";" + x + "H" + s;
    }
    
    public String skin(int type, int x, int y, String s) {
        return (char)27 + "[" + y + ";" + x + "H" +(char)27 + "[" + type + "m" + s + (char)27 + "[" + type + "m";
    }
    
    public long getStartTime() {
        return startTime;
    }
    
    public int getUsersOnline() {
        int tot = 0;
        for (User u : users) {
            if (u != null) tot++;
        }
        return tot;
    }
    
    public int getMostUsers() {
        return most;
    }
    
    public void setMostUsers(int i) {
        most = i;
    }
    
    public int getDamage(int i) {
        return weapon[i][0];
    }
    
    public int getRange(int i) {
        return weapon[i][1];
    }
    
    public int getCost(int i) {
        return weapon[i][2];
    }
    
    private class Listener extends Thread {
        private ServerSocket server;
        private boolean run;
        
        public Listener() {
            try {
                run = true;
                server = new ServerSocket(7100);
            } catch (Exception e) {}
        }
        
        public void run() {
            try {
                while (run) {
                    Socket s = server.accept();
                    addUser(s);
                }
                server.close();
            } catch (Exception e) {}
        }
        
        public void stopListener() {
            try {
                run = false;
                server.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
